import router from './routes-vaccine.js'
import NProgress from 'nprogress'


String.prototype.toJSON = function(){
	return eval('('+this+')');
};
var utils = {
//	rootUrl: "http://localhost:8080/WsM/",   
//	socketUrl:'localhost:8080/WsM/',
//	imageRootUrl:'http://localhost:8080',
//	rootUrl: "http://llz.j2eeall.com/WsM/",
//	socketUrl:'llz.j2eeall.com/WsM/',
//	imageRootUrl:'http://llz.j2eeall.com',
	rootUrl: "http://106.12.85.207:8080/WsM/",
	socketUrl:'106.12.85.207:8080/WsM/',
	imageRootUrl:'http://106.12.85.207:8080',
//	rootUrl: "http://shcfgs.com/WsM/",
//	socketUrl:'shcfgs.com/WsM/',
//	imageRootUrl:'http://shcfgs.com',
	stock:0,
	getQueryString: function(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
		var r = window.location.href.substr(window.location.href.indexOf('?') + 1).match(reg);
		if(r != null) return unescape(r[2]);
		return null;
	},
	ajaxJSON: function(params) {
		params.dataType = "json";
		params.data = JSON.stringify(params.data);
		params.contentType = "application/json;charset=UTF-8";
		params.url = params.url.indexOf('http://') == 0 ? params.url : this.rootUrl + params.url;
		this._ajax(params);
	},
	get: function(params) {
		params.url = params.url.indexOf('http://') == 0 ? params.url : this.rootUrl + params.url;
		params.type = 'get';
		this._ajax(params);
	},
	_ajax: function(params) {
		var _self= this;
		params.beforeSend = function() {
			console.log('ajax beforeSend')
			_self.stock++;
			NProgress.start();
		};
		params.complete = function(res) {
			console.log('ajax complete')
			_self.stock--;
			if(_self.stock == 0){
				NProgress.done();
			}
		};
		params.error = function(res) {
			if(res.status == 666) {
				router.replace({
					path: '/login'
				})
			}
			console.log('ajax error')
			NProgress.done();
		}
		params.xhrFields = {
			withCredentials: true
		}
		$.ajax(params);
	},
	setJsonObject:function(storage_key, item){
		window.localStorage.setItem(storage_key, JSON.stringify(item));
	},
	getJsonObject:function(storage_key){
		return (window.localStorage.getItem(storage_key) || '{}').toJSON();
	}

}

export default utils;